package com.qimo.zuoyeoa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qimo.zuoyeoa.dao.NoticeMapper;
import com.qimo.zuoyeoa.entity.Notice;
import com.qimo.zuoyeoa.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Override
    public Notice findByNamePass(String noticename, String password) {
        return noticeMapper.selectByNamePass(noticename,password);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean append(Notice notice) {
        return noticeMapper.insert(notice) > 0;
    }

    @Override
    public PageInfo<Notice> findAll(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        final List<Notice> notices = noticeMapper.selectAll();
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);
        return pageInfo;
    }

    @Override
    public PageInfo<Notice> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<Notice> notices = noticeMapper.selectByKeyword(keyword);
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);
        return pageInfo;
    }

    @Override
    public Notice findById(int id) {
        return noticeMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Notice notice) {
        return noticeMapper.updateById(notice)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean remove(int id) {
        return noticeMapper.deleteById(id) > 0;
    }

    @Override
    public List<Notice> findAll() {
        return noticeMapper.selectAll();
    }
}
